// pages/bargainDetails/bargainDetails.js
const app = getApp();
let utils = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: 0,        //"滚动视图的高度"
    topIcon: '../../images/bargain/bread.jpg',
    leftTimeStr: [],
    leftTime: 0,
    discountPrice: 12,
    originalPrice: 36,
    bargainedPrice: 4,    //砍掉的价钱
    goodName: '【全麦】芝心大咖',
    stockNum: 69,
    itemId: '111',
    bargainId:'222',
    state: '0',      //0表示活动进行中 1表示该砍价已经结束
    intervalId: -1000,           //倒计时计时器id
    pollInterval: 3,            //轮询间隔的时间
    pollConut: 0,               //轮询计时
    isOffset:false,             //商品名称是否偏移
    bargainTipModal: null
  },

  addInterval() {                           //添加计时器
    if (this.data.intervalId != -1000) return;
    this.data.intervalId = setInterval(this.intervalHandle, 1000);
  },
  intervalHandle() {                        //计时器处理
    this.data.leftTime--;
    if (this.data.leftTime <= 0) {
      this.timeOutHandle();
    }
    this.refreshExistingTime();
    if (this.data.pollConut >= this.data.pollInterval) {
      this.data.pollConut = 0;
      this.refreshRecordsList();
    }
  },
  timeOutHandle() {                //砍价活动时间到了
    this.removeInterval();
    this.setData({ state: '1' });
    console.log('砍价活动时间到了');
  },
  removeInterval() {                     //移除计时器
    if (this.data.intervalId == -1000) return;
    clearInterval(this.data.intervalId);
    this.data.intervalId = -1000;
  },
  refreshExistingTime() {          //刷新倒计时
    let timeChar = utils.secToTime(this.data.leftTime);
    this.setData({ leftTimeStr: timeChar.split(':') });
  },
  lookForRecords(){        //查看砍价记录
    wx.navigateTo({ url: '../bargainRecords/bargainRecords'});
  },
  refreshRecordsList(){         //刷新砍价记录列表

  },
  myCatchTouch() {              //阻止模态框穿透
  },
  onLoad: function (options) {
    if (this.data.goodName.indexOf('【')==0){
      this.setData({ isOffset:true});
    }
    this.data.leftTime = 12345;
    this.refreshExistingTime();
    this.addInterval();
  },

  onReady: function () {
    var query = wx.createSelectorQuery();
    query.select('#bargain-details-bottom').boundingClientRect();
    wx.getSystemInfo({
      success: win_res => {
        //获取滚动条的高度
        query.exec(dom_res => {
          if (!dom_res[0]) this.setData({ scrollHeight: win_res.windowHeight });
          else {
            let bottom_h = dom_res[0].height;
            this.setData({ scrollHeight: win_res.windowHeight - bottom_h });
          }
        })
      }
    });
    //获取模态弹窗
    this.data.bargainTipModal = this.selectComponent("#bargainTipModal");
    setTimeout(()=>{
      this.data.bargainTipModal.showTip(3.2);
    },3000)

  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {
    this.removeInterval();
  },

  onShareAppMessage: function () {
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: "限时团购,等你加入~"
    };
    shareObj.path = '/pages/bargainDetails/bargainDetails?id=' + this.data.bargainId;
    //shareObj.imageUrl = this.data.topIcon;
    return shareObj;
  }
})
